package co.fitstart.entity.user.agent;

import java.beans.Transient;
import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.demo2do.core.format.DecimalFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
 * 
 * @author dan
 *
 */
@Entity
@Table(name = "agent_record")
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class AgentRecord {
    
    @Id
    @GeneratedValue
    private Long id;
    
    /**记录类型 */
    @Enumerated(EnumType.ORDINAL)
    private RecordType recordType;
    
    /** 金额 */
    @DecimalFormat
    private int offer;
    
    /** 发生时间 */
    private Date occurTime;
    
    /** 已提现记录打款时间 */
    private Date payTime;
    
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private Agent agent;
    
    @Column(name = "is_disabled")
    private boolean disabled;
    
    /**
     * The default constructor
     */
    public AgentRecord() {
        
    }
    
    /**
     * 
     * @param agent
     * @param recordType
     * @param offer
     */
    public AgentRecord(Agent agent, RecordType recordType, int offer) {
        this.recordType = recordType;
        this.offer = offer;
        this.occurTime = new Date();
        this.agent = agent;
        this.disabled = false;
    }
    
    /**
     * 
     * @return
     */
    public AgentRecord pay() {
        this.recordType = RecordType.WITHDRAWED;
        this.payTime = new Date();
        return this;
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public BigDecimal getDecimalOffer() {
        return new BigDecimal(offer).divide(new BigDecimal(100));
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public Date getTime() {
        if(recordType.isWithdrawed()) {
            return payTime;
        } else {
            return occurTime;
        }
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the recordType
     */
    public RecordType getRecordType() {
        return recordType;
    }

    /**
     * @return the offer
     */
    public int getOffer() {
        return offer;
    }

    /**
     * @return the occurTime
     */
    public Date getOccurTime() {
        return occurTime;
    }

    /**
     * @return the payTime
     */
    public Date getPayTime() {
        return payTime;
    }

    /**
     * @return the agent
     */
    public Agent getAgent() {
        return agent;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param recordType the recordType to set
     */
    public void setRecordType(RecordType recordType) {
        this.recordType = recordType;
    }

    /**
     * @param offer the offer to set
     */
    public void setOffer(int offer) {
        this.offer = offer;
    }

    /**
     * @param occurTime the occurTime to set
     */
    public void setOccurTime(Date occurTime) {
        this.occurTime = occurTime;
    }

    /**
     * @param payTime the payTime to set
     */
    public void setPayTime(Date payTime) {
        this.payTime = payTime;
    }

    /**
     * @param agent the agent to set
     */
    public void setAgent(Agent agent) {
        this.agent = agent;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

}
